﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
using System.Threading;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using CompressDataSet;
using System.IO.Compression;
using Logistics;
using Logistics.Configuration;

namespace DataUpload
{
    public class HttpServiceObject
    {
        string m_strUrl = "";
        string svrPath = "";
        string upLoad_Method = "HttpRequestUpLoadService.ashx";
        public HttpServiceObject()
        {
            m_strUrl = GlobalObjects.ConfigFileLocation;
            svrPath = XmlFiles.Read(m_strUrl, "/system/RemoteSiteURL", "");
        }

        public string SendSMS(String[] lst_WaybillIds)
        {
            string msg = "";
            if (lst_WaybillIds!=null && lst_WaybillIds.Length > 0)
            {
                string waybillIds = "";
                foreach (string id in lst_WaybillIds)
                {
                    waybillIds += id.Trim().Replace("\'", "") + ",";
                }
                if (!String.IsNullOrEmpty(waybillIds) && waybillIds.EndsWith(","))
                {
                    waybillIds = waybillIds.Substring(0, waybillIds.Length - 1);
                }
                Encoding parEncoding = Encoding.GetEncoding("UTF-8");
                string param = HttpUtility.UrlEncode("method", parEncoding) + "=" + HttpUtility.UrlEncode("SendSMS", parEncoding) + "&" +
                    HttpUtility.UrlEncode("waybillIds", parEncoding) + "=" + HttpUtility.UrlEncode(waybillIds, parEncoding);

                try
                {
                    byte[] bt = Encoding.ASCII.GetBytes(param);
                    XmlDocument xmlDoc = httpRequest(bt, svrPath);
                    parseXmlString(xmlDoc, out msg);
                }
                catch { }
            }
            return msg;
        }

        public string UpLoadData(DataSet ds)
        {
            string msg = "";
            byte[] bArrayResult = null;
            bArrayResult = DataFormat.GetBinaryFormatDataCompress(ds);//序列化后并且压缩的二进制流

            string url = svrPath.Substring(0, svrPath.LastIndexOf('/')) + "/" + upLoad_Method;

            try
            {
                XmlDocument xmlDoc = httpRequest(bArrayResult, url);
                parseXmlString(xmlDoc, out msg);
            }
            catch { }
            return msg;
        }

        #region HttpWebRequest内部调用
        private XmlDocument httpRequest(byte[] bt, string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            request.ContentLength = bt.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(bt, 0, bt.Length);
            }
            using (WebResponse rsponse = request.GetResponse())
            {
                StreamReader reader = new StreamReader(rsponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(reader.ReadToEnd());
                return xmlDoc;
            }
        }


        /// <summary>
        /// 解析XML,返回字符串
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private string parseXmlString(XmlDocument xmlDoc, out string msg)
        {
            XmlNode msgNode = xmlDoc.SelectSingleNode("result/resultmessage");
            msg = msgNode.InnerText;
            XmlNode resultData = xmlDoc.SelectSingleNode("result/resultdata");
            return resultData.InnerText;
        }
        #endregion

    }
}
